<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/12
 * Time: 19:31
 */

namespace app\common\controller;

use app\common\utils\upload\Uploader_c;
use think\facade\Db;

class AjaxController extends BaseController
{
    //文件上传
    public function uploader(){
        if($this->request->isAjax()) {
            $path = $this->request->param('path');
            $dir = $path?'uploads/data/'.$path:'uploads/data';

            include (root_path().'extend/upload/Uploader_c.php');

            \Uploader_c::getIns($dir)->init();
        }
    }

    //获取省市
    public function ajaxGetProvince($id = 0){
        if($id == 0){
            return Db::name('region')->where(['parent_id'=>0])->select();
        }else{
            return Db::name('region')->where(['parent_id'=>$id])->select();
        }
    }

    /*
    * 获取地区
    */
    public function getRegion(){
        $parent_id = input('get.parent_id/d');
        $selected = input('get.selected',0);
        $data = Db::name('region')->where("parent_id",$parent_id)->select();
        $html = '';
        if($data){
            foreach($data as $h){
                if($h['id'] == $selected){
                    $html .= "<option value='{$h['id']}' selected>{$h['name']}</option>";
                }
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        echo $html;
    }


    public function getTwon(){
        $parent_id = input('get.parent_id/d');
        $data = Db::name('region')->where("parent_id",$parent_id)->select();
        $html = '';
        if($data){
            foreach($data as $h){
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        if(empty($html)){
            echo '0';
        }else{
            echo $html;
        }
    }

    /**
     * 获取省
     */
    public function getProvince()
    {
        $province = Db::name('region')->field('id,name')->where(array('level' => 1))->cache(true)->select();
        $res = array('status' => 1, 'msg' => '获取成功', 'result' => $province);
        exit(json_encode($res));
    }

    /**
     * 获取市或者区
     */
    public function getRegionByParentId()
    {
        $parent_id = input('parent_id');
        $res = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
        if($parent_id){
            $region_list = Db::name('region')->field('id,name')->where(['parent_id'=>$parent_id])->select();
            $res = array('status' => 1, 'msg' => '获取成功', 'result' => $region_list);
        }
        exit(json_encode($res));
    }

}